package cn.gong.moviekong.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.gong.moviekong.config.Configs;
import cn.gong.moviekong.manager.ActivityManager;
import cn.gong.moviekong.model.Task;
import cn.gong.moviekong.utils.DebugUtils;

public abstract class BaseActivity extends Activity implements OnClickListener {
	protected int actID;
	protected ActivityManager activityManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		activityManager = ActivityManager.getInstance();
		actID = activityManager.getSize();
		activityManager.addActivity(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		activityManager.removeActivity(this);
	}

	public int getActID() {
		return actID;
	}

	protected void finishThis() {
		finish();
		animationForOld();
	}

	public abstract void refresh(Task task);

	protected void toast(String msg) {
		Toast.makeText(this, msg, Configs.TOAST_TIME).show();
	}

	protected void debug(String msg) {
		DebugUtils.debug(msg);
	}

	protected void exception(Exception e) {
		DebugUtils.exception(e);
	}

	protected void isNull(String name, Object obj) {
		DebugUtils.isNull(name, obj);
	}

	public void animationForNew() {
		overridePendingTransition(R.anim.translatex100to0, R.anim.translatex0tof100);
	}

	public void animationForOld() {
		overridePendingTransition(R.anim.translatexf100to0, R.anim.translatex0to100);
	}

	protected AlertDialog getAlertDialog() {
		AlertDialog myDialog = new AlertDialog.Builder(this).create();
		myDialog.show();
		myDialog.getWindow().setContentView(R.layout.progress);
		return myDialog;
	}
	
	protected void startActForNew(Intent intent) {
		startActivity(intent);
		animationForNew();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finishThis();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	protected LinearLayout findLinearLayoutById(int id) {
		return (LinearLayout) findViewById(id);
	}

	protected LinearLayout findLinearLayoutById(int id, View view) {
		return (LinearLayout) view.findViewById(id);
	}

	protected Button findButtonById(int id) {
		return (Button) findViewById(id);
	}

	protected Button findButtonById(int id, View view) {
		return (Button) view.findViewById(id);
	}

	protected RelativeLayout findRelativeLayoutById(int id) {
		return (RelativeLayout) findViewById(id);
	}

	protected RelativeLayout findRelativeLayoutById(int id, View view) {
		return (RelativeLayout) view.findViewById(id);
	}

	protected TextView findTextViewById(int id) {
		return (TextView) findViewById(id);
	}

	protected TextView findTextViewById(int id, View view) {
		return (TextView) view.findViewById(id);
	}

	protected CheckBox findCheckBoxById(int id) {
		return (CheckBox) findViewById(id);
	}

	protected CheckBox findCheckBoxById(int id, View view) {
		return (CheckBox) view.findViewById(id);
	}

	protected ImageButton findImageButtonById(int id) {
		return (ImageButton) findViewById(id);
	}

	protected ImageButton findImageButtonById(int id, View view) {
		return (ImageButton) view.findViewById(id);
	}

	protected ImageView findImageViewById(int id) {
		return (ImageView) findViewById(id);
	}

	protected ImageView findImageViewById(int id, View view) {
		return (ImageView) view.findViewById(id);
	}

	protected EditText findEditTextById(int id) {
		return (EditText) findViewById(id);
	}

	protected EditText findEditTextById(int id, View view) {
		return (EditText) view.findViewById(id);
	}

	protected void setShow(View v, boolean show) {
		v.setVisibility(show ? View.VISIBLE : View.GONE);
	}
}
